package com.jxs.android.weiget;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.jxs.android.weixi.data.OweFeeDetailInfo;
import com.telecom.weixi.R;

public class BuOweDetailView extends LinearLayout {

	static public interface LoadingOweDataListener {
		public void loadingData(int currentPage);
	}

	private Context context;
	private TextView item_index, item_content_left, item_content_center, item_content_right, null_has_data;
	private ListView listView;
	private int currentPage = 1;
	private LayoutInflater inflater;
	private OweFeeDetailInfoAdapter adapter;

	private View loadMoreView;
	private Button loadMoreButton;
	private LoadingOweDataListener dataListener;

	public BuOweDetailView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		initView();
	}

	public BuOweDetailView(Context context) {
		this(context, null);
	}

	private void initView() {
		inflater = LayoutInflater.from(context);
		inflater.inflate(R.layout.bu_owe_detail_view, this);
		item_index = (TextView) findViewById(R.id.item_index);
		item_content_left = (TextView) findViewById(R.id.item_content_left);
		item_content_center = (TextView) findViewById(R.id.item_content_center);
		item_content_right = (TextView) findViewById(R.id.item_content_right);

		item_index.setTextColor(context.getResources().getColor(R.color.white));
		item_content_left.setTextColor(context.getResources().getColor(R.color.white));
		item_content_center.setTextColor(context.getResources().getColor(R.color.white));
		item_content_right.setTextColor(context.getResources().getColor(R.color.white));

		null_has_data = (TextView) findViewById(R.id.null_has_data);

		loadMoreView = inflater.inflate(R.layout.bu_loading_btn, null);
		loadMoreButton = (Button) loadMoreView.findViewById(R.id.loadMoreButton);
		loadMoreButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				loadMoreButton.setText("正在加载..."); //
				if (null != dataListener) {
					dataListener.loadingData(currentPage);
				}
			}
		});

		listView = (ListView) findViewById(R.id.common_listView);
		listView.addFooterView(loadMoreView);
		adapter = new OweFeeDetailInfoAdapter();
		listView.setAdapter(adapter);
		initTitle("序号", "欠费月份", "虚欠费类型", "欠费金额");
	}

	public void setDataListener(LoadingOweDataListener dataListener) {
		this.dataListener = dataListener;
	}

	public OweFeeDetailInfoAdapter getAdapter() {
		return adapter;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public void setEmpty(String text) {
		null_has_data.setText(text);
		null_has_data.setVisibility(View.VISIBLE);
	}

	public void setEmptyVisibility(int visibility) {
		null_has_data.setVisibility(visibility);
	}

	public void initTitle(String item_index, String item_content_left, String item_content_center, String item_content_right) {
		this.item_index.setText(item_index);
		this.item_content_left.setText(item_content_left);
		this.item_content_center.setText(item_content_center);
		this.item_content_right.setText(item_content_right);

	}

	public void setOweFeeDetailInfos(List<OweFeeDetailInfo> custInfos) {
		adapter.setOweFeeDetailInfos(custInfos);
		adapter.notifyDataSetChanged();
	}

	private class OweFeeDetailInfoAdapter extends BaseAdapter {

		private List<OweFeeDetailInfo> termInfos = new ArrayList<OweFeeDetailInfo>();

		private ViewHolder holder = null;

		public void setOweFeeDetailInfos(List<OweFeeDetailInfo> custInfos) {
			if (currentPage == 1) {
				this.termInfos.clear();
			}
			null_has_data.setVisibility(View.GONE);
			this.termInfos.addAll(custInfos);
		}

		@Override
		public int getCount() {
			return termInfos.size();
		}

		@Override
		public OweFeeDetailInfo getItem(int position) {
			return termInfos.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			OweFeeDetailInfo custInfo = termInfos.get(position);
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.bu_owe_detail_view_item, null);
				holder = new ViewHolder();
				holder.item_index = (TextView) convertView.findViewById(R.id.item_index);
				holder.item_content_left = (TextView) convertView.findViewById(R.id.item_content_left);
				holder.item_content_center = (TextView) convertView.findViewById(R.id.item_content_center);
				holder.item_content_right = (TextView) convertView.findViewById(R.id.item_content_right);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			int resid = R.drawable.table_row1;
			holder.item_index.setBackgroundResource(R.drawable.table_row_index_1);
			holder.item_content_left.setBackgroundResource(resid);
			holder.item_content_center.setBackgroundResource(resid);
			holder.item_content_right.setBackgroundResource(resid);

			holder.item_index.setText(custInfo.pos);
			holder.item_content_left.setText(custInfo.owe_month);
			holder.item_content_center.setText(custInfo.vir_state_name);
			holder.item_content_right.setText(custInfo.content);

			return convertView;
		}

		class ViewHolder {
			TextView item_index;
			TextView item_content_left;
			TextView item_content_center;
			TextView item_content_right;
		}

	}

	public ListView getListView() {
		return listView;
	}

	public View getLoadMoreView() {
		return loadMoreView;
	}

	public Button getLoadMoreButton() {
		return loadMoreButton;
	}

}
